package com.kingviker.example.handler3;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyClientHandler extends SimpleChannelInboundHandler<MyCustomProtocol> {

    private Logger logger = LoggerFactory.getLogger(MyClientHandler.class);
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            byte[] content = ("helloworld!"+i).getBytes();
            MyCustomProtocol customProtocol = new MyCustomProtocol();
            customProtocol.setContent(content);
            customProtocol.setLength(content.length);
            ctx.writeAndFlush(customProtocol);
        }



    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyCustomProtocol msg) throws Exception {
//        ctx.channel().writeAndFlush("from server:"+ UUID.randomUUID().toString());
    }

}
